package com.voltmao.controller.module.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.persistent.model.SysUserInfo;
import com.voltmao.service.application.module.SysUserInfoServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author zhouyang
 * @date 2018/3/29
 */
@Controller
@RequestMapping(value = "/module/sys/user_info")
public class UserInfoController {
    @Autowired
    SysUserInfoServiceImpl sysUserInfoService;


    /**
     * 登录验证
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/authLogin.json", method = RequestMethod.POST)
    public ApiResult authLogin(HttpServletRequest request) {

        /**
         *
         */
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                Map<String, Object> res = new HashMap<>();
                SysUserInfo sysUserInfo = sysUserInfoService.authLogin(params);
                if (sysUserInfo == null) {
                    apiResult.setStatus(Constants.AUTH_FAIVAL);
                } else {
                    apiResult.setStatus(Constants.SUCCESS);

                    //使用sesstion
                    request.getSession().setAttribute("sysUserInfo", sysUserInfo);
                    return sysUserInfoService.getMenuData(sysUserInfo.getDataId());
                }

                return res;
            }
        });
    }

    @ResponseBody
    @RequestMapping(value = "/test.json", method = RequestMethod.GET)
    public ApiResult test(HttpServletRequest request) {
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);


                return sysUserInfoService.getMenuData("1");
            }
        });
    }

    @ResponseBody
    @RequestMapping(value = "/testLoadGridView.json", method = RequestMethod.GET)
    public GridViewResult testGrid(HttpServletRequest request) {
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "sdfsdf.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);

                return sysUserInfoService.selectSysUsersDataPage(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) {
                Map<String, Object> defalutMap = new HashMap<String, Object>();
                defalutMap.put("xx", "123");


                return defalutMap;
            }
        });
    }

    @ResponseBody
    @RequestMapping(value = "/testSeachGrid.json", method = RequestMethod.GET)
    public GridViewResult testSeachGrid(HttpServletRequest request) {

        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);

                return sysUserInfoService.selectSysUsersDataPage(params);
            }
        });
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    @RequestMapping(value = "/updatePwd.json")
    @ResponseBody
    public ApiResult updatePwd(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try{
                    sysUserInfoService.updatePwd(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.FAIVAL);
                    return e.getMessage();
                }

                return null;
            }
        });
    }
}
